package cz.muni.fi.pa165.library.web_layer.adminPages.Book;

import cz.muni.fi.pa165.library.web_layer.adminPages.Book.EditBook;
import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class ShowAllBook extends Template {
    private static final long serialVersionUID = 1L; 
    
    public ShowAllBook(final PageParameters parameters) {
	super(parameters);
        
        // DELETE ITEM
        StringValue bookId = parameters.get("bookId");
        if (!bookId.isEmpty()) {
            BookService bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
            bookService.deleteBook(bookService.findBookById(bookId.toLong()));
            setResponsePage(ShowAllBook.class);
        }
        
       BookService bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
       RepeatingView repeating = new RepeatingView("repeating");
       add(repeating);       
       
       for (BookTO book : bookService.findAllBooks()) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            PageParameters pageParameters = new PageParameters();
            pageParameters.add("bookId", book.getId());
            item.add(new BookmarkablePageLink<Void>("edit", EditBook.class, pageParameters));
            item.add(new BookmarkablePageLink<Void>("delete", ShowAllBook.class, pageParameters));
            item.add(new Label("title", book.getTitle()));
            item.add(new Label("author", book.getAuthor()));
            item.add(new Label("genre", String.valueOf(book.getGenre())));
            item.add(new Label("publicationYear", String.valueOf(book.getPublicationYear())));
            item.add(new Label("publicationPlace", book.getPublicationPlace()));
            item.add(new Label("publisher", book.getPublisher()));
            item.add(new Label("ISBN", book.getISBN()));
            item.add(new Label("status", String.valueOf(book.getStatus())));
            item.add(new Label("availability", String.valueOf(book.getAvailability())));            
           repeating.add(item);
      }
        BookmarkablePageLink addBookLink = new BookmarkablePageLink<>(
                "addBook", EditBook.class);
        add(addBookLink);
        
        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "home", HomePage.class);
        add(homeLink);
    }
}
